package com.yuexunit.mobilenurse.module.SkinTest.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.base.titlebar.TitleBar_DocAdvice;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.Login.bean.SystemConfig;
import com.yuexunit.mobilenurse.module.Login.bean.YXLoginData;
import com.yuexunit.mobilenurse.module.Login.model.impl.LoginModel;
import com.yuexunit.mobilenurse.module.Login.presenter.IUserPresenter;
import com.yuexunit.mobilenurse.module.Login.presenter.impl.UserPresenter;
import com.yuexunit.mobilenurse.module.Login.ui.view.IUserView;
import com.yuexunit.mobilenurse.module.SkinTest.bean.SkinTestDetailNewBean;
import com.yuexunit.mobilenurse.module.SkinTest.bean.YXSkinTestExecBean;
import com.yuexunit.mobilenurse.module.SkinTest.model.impl.SkinTestDetailModel;
import com.yuexunit.mobilenurse.module.SkinTest.presenter.ISkinTestDetailPresenter;
import com.yuexunit.mobilenurse.module.SkinTest.presenter.impl.SkinTestDetailPresenter;
import com.yuexunit.mobilenurse.module.SkinTest.ui.view.ISkinTestDetailView;
import com.yuexunit.mobilenurse.widget.ActionSheetDialog;
import com.yuexunit.mobilenurse.widget.LoginDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.SystemTool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/13 0013.
 */

public class Act_SkinTest_Detail extends TitleBar_DocAdvice implements ISkinTestDetailView, IUserView {
    @Bind(R.id.titlebar_docadvice_tv_title)
    TextView titlebarDocadviceTvTitle;
    @Bind(R.id.docactvice_name)
    TextView docactviceName;
    @Bind(R.id.docactvice_bedno)
    TextView docactviceBedno;
    @Bind(R.id.docactvice_visitno)
    TextView docactviceVisitno;

    @Bind(R.id.edt_isn)
    EditText edtIsn;
    @Bind(R.id.edt_batch)
    EditText edtBatch;
    @Bind(R.id.skin_detail_duration_ll_01)
    LinearLayout skinDetailDurationLl01;
    @Bind(R.id.custom_duration)
    EditText customDuration;
    @Bind(R.id.skin_detail_duration_ll_02)
    RelativeLayout skinDetailDurationLl02;
    @Bind(R.id.skin_start_time)
    TextView skinStartTime;
    @Bind(R.id.skin_start_name)
    TextView skinStartName;
    @Bind(R.id.skin_duration)
    TextView skinDuration;
    @Bind(R.id.skin_detail_start_ll_01)
    LinearLayout skinDetailStartLl01;
    @Bind(R.id.skin_detail_start_ll_02)
    LinearLayout skinDetailStartLl02;

    @Bind(R.id.skin_result)
    TextView skinResult;
    @Bind(R.id.skin_result_name)
    TextView skinResultName;
    @Bind(R.id.skin_result_time)
    TextView skinResultTime;
    @Bind(R.id.skin_detail_end_ll_01)
    LinearLayout skinDetailEndLl01;
    @Bind(R.id.skin_detail_end_ll_02)
    LinearLayout skinDetailEndLl02;
    @Bind(R.id.skin_detail_end_first_ll)
    LinearLayout skinDetailEndFirstLl;

    @Bind(R.id.skin_verify_name)
    TextView skinVerifyName;
    @Bind(R.id.skin_verify_time)
    TextView skinVerifyTime;
    @Bind(R.id.skin_detail_end_ll_03)
    LinearLayout skinDetailEndLl03;
    @Bind(R.id.skin_detail_end_ll_04)
    LinearLayout skinDetailEndLl04;
    @Bind(R.id.skin_detail_end_second_ll)
    LinearLayout skinDetailEndSecondLl;

    @Bind(R.id.skin_review_name)
    TextView skinReviewName;
    @Bind(R.id.skin_review_time)
    TextView skinReviewTime;
    @Bind(R.id.skin_detail_end_ll_05)
    LinearLayout skinDetailEndLl05;
    @Bind(R.id.skin_detail_end_ll_06)
    LinearLayout skinDetailEndLl06;
    @Bind(R.id.skin_detail_end_third_ll)
    LinearLayout skinDetailEndThirdLl;

    @Bind(R.id.skintestdetail_selecttime_btn_01)
    TextView skintestdetailSelecttimeBtn01;
    @Bind(R.id.skintestdetail_selecttime_btn_02)
    TextView skintestdetailSelecttimeBtn02;
    @Bind(R.id.skintestdetail_selecttime_btn_03)
    TextView skintestdetailSelecttimeBtn03;

    private String Visitno, psh;
    Boolean isResult = false;
    ISkinTestDetailPresenter presenter;

    private boolean isEdit = true;

    LoginDialog loginDialog;
    private IUserPresenter userPresenter;

    private String time = "";

    private String type = "";

    @Override
    public void setRootView() {
        setContentView(R.layout.act_skintest_detail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        userPresenter = new UserPresenter(this, new LoginModel());
        presenter = new SkinTestDetailPresenter(this, new SkinTestDetailModel());
        Visitno = PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_VISITNO);
        psh = getIntent().getExtras().getString("psh");
        if (SystemTool.checkNet(aty)) {
            presenter.getSkinTestDetailData(psh);
        } else {
            ViewInject.toast("网络异常，请检查网络是否连接!");
        }
    }

    @Override
    public void setTitle() {
        titlebarDocadviceTvTitle.setText("皮试详情");
        docactviceName.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_NAME));
        docactviceBedno.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_BEDCODE));
        docactviceVisitno.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_VISITNO));

    }

    @Override
    public void onBackClick() {
        finish();
    }

    @OnClick({R.id.skintestdetail_selecttime_btn_01, R.id.skintestdetail_selecttime_btn_02, R.id.skintestdetail_selecttime_btn_03, R.id.skin_start, R.id.skin_result_ll, R.id.skin_end, R.id.skin_verify, R.id.skin_review})
    public void onViewClicked(View view) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());
        switch (view.getId()) {
            case R.id.skintestdetail_selecttime_btn_01:
                time = "10";
                skintestdetailSelecttimeBtn01.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_selected);
                skintestdetailSelecttimeBtn01.setTextColor(getResources().getColor(R.color.white));
                skintestdetailSelecttimeBtn02.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_normal);
                skintestdetailSelecttimeBtn02.setTextColor(getResources().getColor(R.color.test_05));
                skintestdetailSelecttimeBtn03.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_normal);
                skintestdetailSelecttimeBtn03.setTextColor(getResources().getColor(R.color.test_05));

                skinDetailDurationLl02.setVisibility(View.GONE);
                break;
            case R.id.skintestdetail_selecttime_btn_02:
                time = "20";
                skintestdetailSelecttimeBtn01.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_normal);
                skintestdetailSelecttimeBtn01.setTextColor(getResources().getColor(R.color.test_05));
                skintestdetailSelecttimeBtn02.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_selected);
                skintestdetailSelecttimeBtn02.setTextColor(getResources().getColor(R.color.white));
                skintestdetailSelecttimeBtn03.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_normal);
                skintestdetailSelecttimeBtn03.setTextColor(getResources().getColor(R.color.test_05));

                skinDetailDurationLl02.setVisibility(View.GONE);
                break;
            case R.id.skintestdetail_selecttime_btn_03:
                time = "";
                skintestdetailSelecttimeBtn01.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_normal);
                skintestdetailSelecttimeBtn01.setTextColor(getResources().getColor(R.color.test_05));
                skintestdetailSelecttimeBtn02.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_normal);
                skintestdetailSelecttimeBtn02.setTextColor(getResources().getColor(R.color.test_05));
                skintestdetailSelecttimeBtn03.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_selected);
                skintestdetailSelecttimeBtn03.setTextColor(getResources().getColor(R.color.white));

                skinDetailDurationLl02.setVisibility(View.VISIBLE);
                break;
            case R.id.skin_start:
                if (time.equals("")) {
                    time = customDuration.getText().toString();
                    if (time.equals("")) {
                        ViewInject.toast("请选择皮试时长");
                    } else {
                        presenter.startSkinTest(psh, PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID),
                                now, time, "", "");
                    }
                } else {
                    presenter.startSkinTest(psh, PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID),
                            now, time, "", "");
                }
                break;
            case R.id.skin_result_ll:
                if (!isResult)
                {
                    selectIdea();
                }
                break;
            case R.id.skin_end:
               if(skinResult.getText().toString().length()>0) {
                   String result ="";
                   if (skinResult.getText().toString().equals("阳性"))
                   {
                       result = "P";
                   }
                   else if (skinResult.getText().toString().equals("阴性")){
                       result = "N";
                   }
                   presenter.sureSkinTest(psh, PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID),
                           now, result,"1");
               }
               else {
                   ViewInject.toast("请选择皮试结果");
               }
                break;
            case R.id.skin_verify:
                loginDialog = new LoginDialog(aty).builder();
                loginDialog.setCallBack(new LoginDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        if(bundle != null){
                            type = "1";
                            userPresenter.doInLogin(bundle.getString("person_num"), bundle.getString("person_psw"));
                        }
                    }
                });
                loginDialog.show();
                break;
            case R.id.skin_review:
                loginDialog = new LoginDialog(aty).builder();
                loginDialog.setCallBack(new LoginDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        if(bundle != null){
                            type = "2";
                            userPresenter.doInLogin(bundle.getString("person_num"), bundle.getString("person_psw"));
                        }
                    }
                });
                loginDialog.show();
                break;
        }
    }

    private void selectIdea(){
        new ActionSheetDialog(aty)
                .builder()
                .setCancelable(false)
                .setCanceledOnTouchOutside(false)
                .addSheetItem("阳性", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                skinResult.setText("阳性");
                            }
                        })
                .addSheetItem("阴性", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                skinResult.setText("阴性");
                            }
                        }).show();
//                .addSheetItem("T", ActionSheetDialog.SheetItemColor.Blue,
//                        new ActionSheetDialog.OnSheetItemClickListener() {
//                            @Override
//                            public void onClick(int which) {
//                                skinResult.setText("T");
//                            }
//                        }).show();
    }

    @Override
    public void showSkinTestDetail(SkinTestDetailNewBean skinTestDetailNewBean) {
        if (null != skinTestDetailNewBean.getPsry()) {
            skinDetailDurationLl01.setVisibility(View.GONE);
            skinDetailDurationLl02.setVisibility(View.GONE);
            skinDetailStartLl02.setVisibility(View.GONE);
            skinDetailStartLl01.setVisibility(View.VISIBLE);
            skinStartTime.setText(skinTestDetailNewBean.getPskssj());
            skinStartName.setText(skinTestDetailNewBean.getPsry());
            skinDuration.setText(skinTestDetailNewBean.getPssc() + "分");

            skinDetailEndFirstLl.setVisibility(View.VISIBLE);
        } else {
            skintestdetailSelecttimeBtn01.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_normal);
            skintestdetailSelecttimeBtn01.setTextColor(getResources().getColor(R.color.test_05));
            skintestdetailSelecttimeBtn02.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_normal);
            skintestdetailSelecttimeBtn02.setTextColor(getResources().getColor(R.color.test_05));
            skintestdetailSelecttimeBtn03.setBackgroundResource(R.drawable.skintestdetail_selecttime_btn_normal);
            skintestdetailSelecttimeBtn03.setTextColor(getResources().getColor(R.color.test_05));
            customDuration.setText("");

            skinDetailDurationLl01.setVisibility(View.VISIBLE);
            skinDetailDurationLl02.setVisibility(View.GONE);
            skinDetailStartLl02.setVisibility(View.VISIBLE);
            skinDetailStartLl01.setVisibility(View.GONE);

            skinDetailEndFirstLl.setVisibility(View.GONE);
        }

        if (null != skinTestDetailNewBean.getPsjg()) {
            skinDetailEndLl01.setVisibility(View.VISIBLE);
            skinDetailEndLl02.setVisibility(View.GONE);

//            skinResult.setText(skinTestDetailNewBean.getPsjg());
            if (skinTestDetailNewBean.getPsjg().equals("N")) {
                skinResult.setText("阴性");
            } else if (skinTestDetailNewBean.getPsjg().equals("P")) {
                skinResult.setText("阳性");
            }
            isResult = true;

            skinResultName.setText(skinTestDetailNewBean.getPsjgqrmz());
            skinResultTime.setText(skinTestDetailNewBean.getPsjgsj());

            skinDetailEndSecondLl.setVisibility(View.VISIBLE);
        } else {
            skinDetailEndLl01.setVisibility(View.GONE);
            skinDetailEndLl02.setVisibility(View.VISIBLE);
            isResult = false;
        }

        if (null != skinTestDetailNewBean.getPsjgshmz()) {
            skinDetailEndLl03.setVisibility(View.VISIBLE);
            skinDetailEndLl04.setVisibility(View.GONE);

            skinVerifyName.setText(skinTestDetailNewBean.getPsjgshmz());
            skinVerifyTime.setText(skinTestDetailNewBean.getPsshsj());

            skinDetailEndThirdLl.setVisibility(View.VISIBLE);
        } else {
            skinDetailEndLl03.setVisibility(View.GONE);
            skinDetailEndLl04.setVisibility(View.VISIBLE);
        }

        if (null != skinTestDetailNewBean.getFhsj()) {
            skinDetailEndLl05.setVisibility(View.VISIBLE);
            skinDetailEndLl06.setVisibility(View.GONE);

            skinReviewName.setText(skinTestDetailNewBean.getFhmz());
            skinReviewTime.setText(skinTestDetailNewBean.getFhsj());
        } else {
            skinDetailEndLl05.setVisibility(View.GONE);
            skinDetailEndLl06.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void startSkinTest(YXSkinTestExecBean yxSkinTestExecBean) {
        if (yxSkinTestExecBean.getBody().getResponse().getHead().getRet_info().equals("成功")) {
            presenter.getSkinTestDetailData(psh);
        } else {
            ViewInject.toast("开始皮试失败:" + yxSkinTestExecBean.getBody().getResponse().getHead().getRet_info());
        }
    }

    @Override
    public void sureSkinTest(YXSkinTestExecBean yxSkinTestExecBean) {
        if (yxSkinTestExecBean.getBody().getResponse().getHead().getRet_info().equals("成功")) {
            presenter.getSkinTestDetailData(psh);
        } else {
            ViewInject.toast("确认结果失败:" + yxSkinTestExecBean.getBody().getResponse().getHead().getRet_info());
        }
    }

    @Override
    public void verifySkinTest(YXSkinTestExecBean yxSkinTestExecBean) {
        if (yxSkinTestExecBean.getBody().getResponse().getHead().getRet_info().equals("成功")) {
            presenter.getSkinTestDetailData(psh);
        } else {
            ViewInject.toast("审核结果失败:" + yxSkinTestExecBean.getBody().getResponse().getHead().getRet_info());
        }
    }

    @Override
    public void reviewSkinTest(YXSkinTestExecBean yxSkinTestExecBean) {
        if (yxSkinTestExecBean.getBody().getResponse().getHead().getRet_info().equals("成功")) {
            presenter.getSkinTestDetailData(psh);
        } else {
            ViewInject.toast("复核结果失败:" + yxSkinTestExecBean.getBody().getResponse().getHead().getRet_info());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {

    }

    @Override
    public void loadingToastStatus(String Content) {

    }

    @Override
    public void doInLogin(YXLoginData yxLoginData) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());
        loginDialog.dismiss();
        if(type.equals("1"))
        {
            presenter.verifySkinTest(psh, PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID),
                    now, "","2");
        }
        else if(type.equals("2")){
            presenter.reviewSkinTest(psh, PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID),
                    now, "","3");
        }
    }

    @Override
    public void saveSystemListParams(SystemConfig systemConfig) {

    }
}
